import 'package:package_info_plus/package_info_plus.dart';

import '../../../../../helper/global/user/logic.dart';
import '../../../../../helper/utils/httputils/http_man.dart';
import '../../../../../helper/utils/httputils/urlpath.dart';
import '../../../../../helper/utils/util.dart';
import '../../../../../helper/utils/validate.dart';
import 'state.dart';

class NewPwdController extends GetxController {
  final state = NewPwdState();
  void actionSure() {
    if (state.account.isEmpty) {
      Hud.toast('请输入手机号');
      return;
    }

    if (!Validate.matchPhone(input: state.account)) {
      Hud.toast('请输入正确的手机号');
      return;
    }
    if (state.code.isEmpty) {
      Hud.toast('请输入验证码');
      return;
    }
    // if (state.oldPwd.isEmpty) {
    //   Hud.toast('请输入原密码');
    //   return;
    // }
    if (state.pwd.isEmpty) {
      Hud.toast('请输入新密码');
      return;
    }
    if (state.pwd1.isEmpty) {
      Hud.toast('请输入确认密码');
      return;
    }
    if (state.pwd1 != state.pwd) {
      Hud.toast('两次密码输入不一致，请重新输入');
      return;
    }

    if (!Validate.matchPwd(input: state.pwd)) {
      Hud.toast('请输入至少8位密码，密码必须包含大小写');
      return;
    }
    requestForForgotPwd();
  }

  void requestForForgotPwd() async {
    var res = await HttpMan.instance.post(UrlPath.updatePwd, args: {
      "newPwd": state.pwd,
      "repeatPwd": state.pwd1,
      "smCode": state.code,
      "telephone": state.account,
      "type": "2",
      "tenantId": "",
    });
    if (res.isSuccess) {
      toSuccess();
    }
  }

  Future<bool> requestForSendCode() async {
    if (state.account.isEmpty) {
      Hud.toast('请输入手机号');
      return false;
    }

    if (!Validate.matchPhone(input: state.account)) {
      Hud.toast('请输入正确的手机号');
      return false;
    }
    var res = await HttpMan.instance.post(UrlPath.sendCode, args: {
      "check": "1",
      "oneParam": state.account,
    });
    if (res.isSuccess) {
      Hud.toast('已发送');
      return true;
    }
    Hud.toast(res.msg);
    return false;
  }

  void toSuccess() {
    Get.offAndToNamed(RouterPath.find_back_success);
  }

  @override
  void onInit() {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      state.appName = packageInfo.appName;
      update();
    });

    super.onInit();
  }
}
